package com.leiduoduo.oms.controller;

import com.leiduoduo.oms.annotation.UserLoginToken;
import com.leiduoduo.oms.pojo.Dish;
import com.leiduoduo.oms.pojo.DishType;
import com.leiduoduo.oms.pojo.Restaurant;
import com.leiduoduo.oms.pojo.Table;
import com.leiduoduo.oms.service.DishService;
import com.leiduoduo.oms.service.DishTypeService;
import com.leiduoduo.oms.service.RestaurantService;
import com.leiduoduo.oms.service.TableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class DishController {
    @Autowired
    DishService dishService;
    @Autowired
    DishTypeService dishTypeService;
    @Autowired
    RestaurantService restaurantService;
    @Autowired
    TableService tableService;

    @PostMapping("/addDish")
    @UserLoginToken
    public boolean addDish(Dish dish) {
        return dishService.addDish(dish);
    }
    @PostMapping("/delDish")
    @UserLoginToken
    public boolean delDish(Dish dish){
        return dishService.delDish(dish);
    }
    @PostMapping("/upDish")
    @UserLoginToken
    public boolean upDish(Dish dish){
        return dishService.upDish(dish);
    }
    @GetMapping("/getDishById/{id}")
    @UserLoginToken
    public Dish getDishById(@PathVariable("id") int id) {
        return dishService.getDishById(id);
    }
    @GetMapping("/getDishList")
    @UserLoginToken
    public List<Dish> getDishList(){
        return dishService.getDishList();
    }
    @GetMapping("/getMenu")
    @UserLoginToken
    public Map<String,Object> getMenu(int table_id){
        Map<String,Object> result = new HashMap<>();
        Map<String,Object> data = new HashMap<>();
        Table table = tableService.getTableById(table_id);
        Restaurant restaurant = restaurantService.getRestaurantById(table.getRestaurant_id());
        List<Dish> dishList = dishService.getDishListByRestId(restaurant.getId());
        List<DishType> dishTypeList = dishTypeService.getDishTypeByRestId(restaurant.getId());
        data.put("dish",dishList);
        data.put("dishType",dishTypeList);
        result.put("data",data);
        result.put("errorcode",0);
        result.put("msg","success");
        return result;
    }
}
